﻿using System;
using System.Drawing;
using System.Configuration.Provider;
using DaveSexton.Web.Controls;

namespace AIP
{
	public sealed class BlueOverlayAutoInputProtectionFilterProvider : AutoInputProtectionFilterProvider
	{
		public float Opacity { get; set; }

		public override bool CanPreProcess
		{
			get { return false; }
		}

		public override bool CanPostProcess
		{
			get { return true; }
		}

		public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
		{
			if (config == null)
				throw new ArgumentNullException("config");

			if (string.IsNullOrEmpty(name))
				name = "SolidColorAutoInputProtectionFilterProvider";

			if (string.IsNullOrEmpty(config["description"]))
			{
				config.Remove("description");
				config.Add("description", "Solid Color Auto-Input Protection Filter Provider");
			}

			base.Initialize(name, config);

			ProviderHelper helper = new ProviderHelper(config);

			Opacity = helper.ParseSingle("opacity", false, .15F);

			// Throw an exception if unrecognized attributes remain
			if (config.Count > 0)
			{
				string attr = config.GetKey(0);

				if (!string.IsNullOrEmpty(attr))
					throw new ProviderException("Unrecognized attribute: " + attr);
			}
		}

		public override void PreProcess(System.Drawing.Graphics graphics, System.Drawing.Size imageSize)
		{
			throw new NotSupportedException();
		}

		public override void PostProcess(System.Drawing.Graphics graphics, System.Drawing.Size imageSize)
		{
			using (Brush brush = new SolidBrush(Color.FromArgb((int) (Opacity * 255), Color.Blue)))
			{
				graphics.FillRectangle(brush, 0, 0, imageSize.Width, imageSize.Height);
			}
		}
	}
}